﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BUS;
using DTO;
using System.Drawing;


namespace FastDeal.Admin
{
    public partial class DanhSachKhuyenMai : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["DaDangNhap"] == null || (bool)Session["DaDangNhap"] == false)
            {
                Response.Redirect("~/Default.aspx");
            }
            if ((int)Session["LoaiThanhVien"] != 1)
            {
                Response.Redirect("~/Default.aspx");
            }

            if (!IsPostBack)
                LoadDanhSachKhuyenMai();
        }

        /// <summary>
        /// Phương thức load danh sách khuyến mãi
        /// </summary>
        private void LoadDanhSachKhuyenMai()
        {
            KhuyenMai_BUS db = new KhuyenMai_BUS();
            List<KhuyenMai> list = new List<KhuyenMai>();
            list = db.LayDanhSach_KhuyenMai();

            gvDanhSachKhuyenMai.DataSource = list;
            gvDanhSachKhuyenMai.DataBind();

            //sửa một số thông tin điền vào grid view
            int i=0;
            foreach (GridViewRow dr in gvDanhSachKhuyenMai.Rows)
            {
                //sửa cột ngày bắt đầu - cột 2
                DateTime dt1 = DateTime.Parse(gvDanhSachKhuyenMai.Rows[i].Cells[2].Text);
                gvDanhSachKhuyenMai.Rows[i].Cells[2].Text = dt1.ToShortDateString();

                //sửa cột ngày kết thúc - cột 3
                DateTime dt2 = DateTime.Parse(gvDanhSachKhuyenMai.Rows[i].Cells[3].Text);
                gvDanhSachKhuyenMai.Rows[i].Cells[3].Text = dt2.ToShortDateString();

                //sửa tình trạng trạng cột
                int tinhtrang = int.Parse(gvDanhSachKhuyenMai.Rows[i].Cells[4].Text);
                if (tinhtrang == 0)
                {
                    gvDanhSachKhuyenMai.Rows[i].Cells[4].Text = "Chưa duyệt";
                    gvDanhSachKhuyenMai.Rows[i].Cells[0].ForeColor = Color.Red;
                    gvDanhSachKhuyenMai.Rows[i].Cells[1].ForeColor = Color.Red;
                    gvDanhSachKhuyenMai.Rows[i].Cells[2].ForeColor = Color.Red;
                    gvDanhSachKhuyenMai.Rows[i].Cells[3].ForeColor = Color.Red;
                    gvDanhSachKhuyenMai.Rows[i].Cells[4].ForeColor = Color.Red;
                    gvDanhSachKhuyenMai.Rows[i].Cells[5].ForeColor = Color.Red;

                }
                else if (tinhtrang == 1)
                {
                    gvDanhSachKhuyenMai.Rows[i].Cells[4].Text = "Đã duyệt";
                    gvDanhSachKhuyenMai.Rows[i].Cells[4].ForeColor = Color.Gray;
                }
                //sửa mã doanh nghiệp thành username
                int id = int.Parse(gvDanhSachKhuyenMai.Rows[i].Cells[5].Text);
                ThongTinTaiKhoan_BUS db2 = new ThongTinTaiKhoan_BUS();
                ThongTinTaiKhoan tt = db2.LayThongTin_ThongTinTaiKhoan(id);
                gvDanhSachKhuyenMai.Rows[i].Cells[5].Text = tt.HoTen;

                i++;
            }
            if (Panel1.Height.Value < 800)
                Panel1.Height = 800;

            lbTongSoKhuyenMai.Text = "Tổng số khuyến mãi: " + list.Count.ToString();
        }

        protected void gvDanhSachKhuyenMai_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void gvDanhSachKhuyenMai_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvDanhSachKhuyenMai.PageIndex = e.NewPageIndex;
            LoadDanhSachKhuyenMai();
        }

        /// <summary>
        /// Sự kiện xóa 1 dòng
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvDanhSachKhuyenMai_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            try
            {
                //Lấy mã để xóa
                int ma = int.Parse(gvDanhSachKhuyenMai.Rows[e.RowIndex].Cells[0].Text);
                KhuyenMai_BUS db = new KhuyenMai_BUS();
                KhuyenMai km = new KhuyenMai();
                km.MaKhuyenMai =  ma;
                db.Xoa_KhuyenMai(km);
                LoadDanhSachKhuyenMai();
            }
            catch
            {

            }
        }
    }
}